package cn.imnaer.service.rpc.share.process;

import cn.imnaer.service.persistence.model.SharePhoto;
import cn.imnaer.service.persistence.model.consts.SHAREPHOTO;
import cn.imnaer.service.rpc.common.BaseContainImageAdapter;
import cn.imnaer.service.rpc.common.BaseService;
import cn.imnaer.service.rpc.common.ICollectCountSerice;
import cn.imnaer.service.rpc.share.dao.SharePhotoDaoImpl;

public class SharePhotoService implements ICollectCountSerice<SharePhoto>, BaseService<SharePhoto> {
	private SharePhotoDaoImpl sharePhotoDaoImpl = SharePhotoDaoImpl.getInstance();;
	private SharePhoto sharePhoto;

	private SharePhotoService(SharePhotoContext context) {
		this.sharePhoto = getModelEntity(context);
	}

	private SharePhotoService() {

	}

	public static SharePhotoService getInstance() {
		return new SharePhotoService();
	}

	public static SharePhotoService getInstance(SharePhotoContext context) {
		return new SharePhotoService(context);
	}

	private SharePhoto getModelEntity(SharePhotoContext context) {
		SharePhoto sharePhoto = null;
		if (context.getSharePhotoId() != null && !context.getSharePhotoId().isEmpty()) {
			sharePhoto = this.sharePhotoDaoImpl.findOneByPk(context.getSharePhotoId());
		} else if (context.getSharePhotoItemId() != null && !context.getSharePhotoItemId().isEmpty()) {
			sharePhoto = this.sharePhotoDaoImpl.findBySharePhotoItemId(context.getSharePhotoItemId());

		} else {
			throw new NullPointerException("sharePhotoId or sharePhotoItemId is null");
		}
		if (sharePhoto == null) {
			throw new NullPointerException("sharePhoto is null");
		}
		return sharePhoto;
	}

	@Override
	public void updateCollectCount(boolean flag) {

		int collectCount = sharePhoto.getCollectCount();
		if (flag) {
			collectCount++;
		} else {
			collectCount--;
		}
		sharePhoto.setCollectCount(collectCount);
		this.sharePhotoDaoImpl.update(this.sharePhoto);

	}

	@Override
	public void updateCollectCountByPk(String pkValue, boolean flag) {
		if (flag) {
			this.sharePhotoDaoImpl.incrementCounterByPk(pkValue, SHAREPHOTO.collectCount);
		} else {
			this.sharePhotoDaoImpl.decrementCounterByPk(pkValue, SHAREPHOTO.collectCount);
		}
	}

	@Override
	public void updateCollectCount(SharePhoto t, boolean flag) {
		// TODO Auto-generated method stub

	}

	@Override
	public SharePhoto save(SharePhoto object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseContainImageAdapter<SharePhoto> getAdapter() {
		return SharePhotoAdapter.newInstance(this.sharePhoto);
	}

	
}
